const express = require("express");

const router = express.Router();
const { ValidationError } = require("../utils/error");

const {
  findAllBannerService,
  updateBannerService,
  createBannerService,
  deleteBannerService,
  findByIdBannerService,
} = require("../services/bannerServices");

const { uploadImageService } = require("../services/uploadServices");

router.post("/", async (req, res) => {
  res.send(await createBannerService(req.body));
});

router.delete("/:id", async (req, res) => {
  if (!req.params.id) throw new ValidationError("id is required");
  res.send(await deleteBannerService(req.params.id));
});

router.put("/:id", async (req, res) => {
  if (!req.params.id) throw new ValidationError("id is required");
  res.send(await updateBannerService(req.params.id, req.body));
});

router.get("/:id", async (req, res) => {
  if (!req.params.id) throw new ValidationError("id is required");
  res.send(await findByIdBannerService(req.params.id));
});

router.get("/query/findAll", async (req, res) => {
  res.send(await findAllBannerService());
});

router.post(
  "/upload/:id",
  (req, res, next) => {
    if (!req.params.id) throw new ValidationError("id is required");
    next();
  },
  uploadImageService
);

module.exports = router;
